﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
//using System.Threading;
//using System.Threading.Tasks;

namespace hw1.hw03
{
    class AlarmClock
    {
        private Timer timer;
        private DateTime? alarmTime;

        public event EventHandler<TickEventArgs> Tick;
        public event EventHandler<AlarmEventArgs> Alarm;

        public AlarmClock()
        {
            timer = new Timer(1000); // 1秒间隔
            timer.Elapsed += TimerElapsed;
            timer.AutoReset = true;
            timer.Enabled = true;
        }

        public void SetAlarmTime(DateTime time)
        {
            alarmTime = time;
        }

        private void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            DateTime now = DateTime.Now;
            Tick?.Invoke(this, new TickEventArgs { CurrentTime = now });

            if (alarmTime.HasValue && now >= alarmTime.Value)
            {
                Alarm?.Invoke(this, new AlarmEventArgs { AlarmTime = alarmTime.Value });
                this.timer.Enabled = false;
                System.Threading.Thread.Sleep(2000);
                Environment.Exit(0);
            }
        }
    }

    public class TickEventArgs : EventArgs
    {
        public DateTime CurrentTime { get; set; }
        public delegate void TickEventHandler(object obj, TickEventArgs e);
    }

    public class AlarmEventArgs : EventArgs
    {
        public DateTime AlarmTime { get; set; }
        public delegate void AlarmEventHandler(object obj, TickEventArgs e);
    }
}
